#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# －－－－jimi－－－－
#  文件名：main.py
#  版本：V2.0
#  author: jimi
#  说明：步进电机实验
'''
s1.step(100)
s1.step(100,-1)
s1.angle(180)
s1.angle(360,-1)
'''
# step设置509差不多angle设置360
#####################################################
import os
import Stepper
import time,ntptime,network
from machine import Pin
from utime import sleep



s1 = Stepper.create(Pin(14,Pin.OUT),Pin(27,Pin.OUT),Pin(26,Pin.OUT),Pin(25,Pin.OUT), delay=2)
touch_DO = Pin(17, Pin.IN, Pin.PULL_UP)
makerobo_tmp = 0    #是否有触摸判断

# 步进电机旋转
def makerobo_rotary(clb_direction):
    if(clb_direction == 'a'):     # 逆时针旋转
        s1.angle(360,-1)
    elif(clb_direction == 'c'):    # 顺时针旋转
        s1.angle(360)
        
# 步进电机旋转90度
def rotate_angle():
    s1.angle(90,-1)


# 打印函数，显示出是否有触摸
def touch_button(x):
    global makerobo_tmp
    if x != makerobo_tmp:
        if x == 1:    #  发生触摸
            print ('*************************')
            print ('* Makerobo Have a touch *')
            print ('*************************')

        if x == 0:    # 没有发生触摸
            print ('*****************************')
            print ('* Makerobo No touch occurred*')
            print ('*****************************')
        makerobo_tmp = x  # 触摸状态值保存和下次做比较，避免重复打印
    
# 循环函数
def makerobo_loop():
    while True:
        clb_direction = input('Makerobo select motor direction a=anticlockwise, c=clockwise: ')
        if(clb_direction == 'c'):
            print('Makerobo motor running clockwise\n')       # 顺时针旋转
            break
        elif(clb_direction == 'a'):
            print('Makerobo motor running anti-clockwise\n')  # 逆时针旋转
            break
        else:
            print('Makerobo input error, please try again!')  # 输入错误，再次输入
    while True:
        makerobo_rotary(clb_direction)       # 让步进电机旋转


#定时启动喂食程序
def feed_pet():
    wifi_main()
    while True:   #时间校准
        try:
            print('time ing')
            ntptime.settime()
            print('time ok')
            mytime=time.localtime()
            print(mytime)
            
            #fulltime='%d-%d-%d %d:%d:%d'%(mytime[0],mytime[1],mytime[2],mytime[3]+8,mytime[4],mytime[5])
            #print(fulltime)
            
            feedtime = '%d:%d'%(mytime[3]+8,mytime[4])
            print(feedtime)
            if(feedtime=='12:20'):
                s1.angle(182,-1)
               # s1.angle(50)
            #break;
            global makerobo_tmp
            if touch_DO.value() != makerobo_tmp:
                if touch_DO.value() == 1:    #  发生触摸
                    s1.angle(182,-1)
                    print ('* Have a touch *')
             #   if touch_DO.value() == 0:    # 没有发生触摸
              #      s1.angle(90)
              #      print ('* No touch occurred*')
            makerobo_tmp = x  # 触摸状态值保存和下次做比较，避免重复打印
        except:
            print('time no')
            time.sleep(1)

        
#连接家里wifi
def wifi_main():
    global inif
    wifi=network.WLAN(network.STA_IF)        #WIFI模式
    if not wifi.isconnected():
        print('wifi  ing......')
        wifi.active(True)
        wifi.connect('huanying','Hyn2022!')  #连接WIFI
        while not wifi.isconnected():
            pass
    print('Wifi connection succeeded')
    print('network config:', wifi.ifconfig())

# 读取配置文件
def readfile():
    os.listdir()

    file = open('feedpet.conf', 'r')
    dataset = file.read().split(":").line.split("\n")
    print (dataset)
    file.close()
    return dataset

# 写入配置文件
def writefile(time):
    file = open('feedpet.conf', 'w')
    file.write(time)
    file.close()
    print ('config writed')



# 程序入口
if __name__ == '__main__':
    # makerobo_loop()  # 循环函数
    feed_pet()
    
 